﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using ThuPhap.ViewModels;
using ThuPhap.Models;
using System.Windows.Media.Imaging;
using Microsoft.Phone.Tasks;

namespace ThuPhap
{
    public partial class SelectBackground : PhoneApplicationPage
    {
        public delegate void FinishedChooseBackground(WriteableBitmap Bitmap);
        public static event FinishedChooseBackground FinishedChooseBackgroundEvent;
        public static string ColorString = "255,255,255,255";
        public SelectBackground()
        {
            InitializeComponent();
            DataContext = App.BackgroundViewModel;
            if (!App.BackgroundViewModel.IsLoaded)
            {
                App.BackgroundViewModel.LoadBackground();
            }

            ChooseColorPage.FinishChooseColorEvent += new ChooseColorPage.FinishChooseColorDelegate(ChooseColorPage_FinishChooseColorEvent);
            ChooseImage.FinishChooseImageEvent += new ChooseImage.FinishChooseImageDelegate(ChooseImage_FinishChooseImageEvent);
            
        }

        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            BackgroundItem backgroundItem = BackgroundItemListBox.SelectedItem as BackgroundItem;
            if (BackgroundItemListBox.SelectedIndex == -1)
            {
                return;
            }
            if (backgroundItem != null)
            {
                string name = backgroundItem.Name;
                if (name == "Background color")
                {
                    NavigationService.Navigate(new Uri("/ChooseColorPage.xaml?color=" + ColorString + "&type=background", UriKind.Relative));
                }
                else if (name == "Gallery image")
                {
                    PhotoChooserTask photoChooser = new PhotoChooserTask();
                    photoChooser.ShowCamera = true;
                    photoChooser.Show();
                    photoChooser.Completed += new EventHandler<PhotoResult>(photoChooser_Completed);
                }
                else
                {
                    WriteableBitmap wb = new WriteableBitmap(0, 0).FromResource(backgroundItem.Image);
                    if (FinishedChooseBackgroundEvent != null)
                    {
                        FinishedChooseBackgroundEvent(wb);
                        NavigationService.GoBack();
                    }
                }

                BackgroundItemListBox.SelectedIndex = -1;
            }
        }

        void photoChooser_Completed(object sender, PhotoResult e)
        {
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                if (e.ChosenPhoto != null)
                {
                    ChooseImage.PhotoStream = e.ChosenPhoto;
                    NavigationService.Navigate(new Uri("/ChooseImage.xaml", UriKind.Relative));

                }
            });
        }

        void ChooseImage_FinishChooseImageEvent(BitmapSource Image)
        {
            if (FinishedChooseBackgroundEvent!=null)
            {
                FinishedChooseBackgroundEvent(new WriteableBitmap(Image));
            }
        }

        void ChooseColorPage_FinishChooseColorEvent(Color Color)
        {
            WriteableBitmap bitmap = new WriteableBitmap(480, 800);
            if (FinishedChooseBackgroundEvent != null)
            {
                SolidColorBrush brush = new SolidColorBrush(Color);
                ColorString = string.Format("{0},{1},{2},{3}", brush.Color.A, brush.Color.R, brush.Color.G, brush.Color.B);
                bitmap.Clear(Color);
                FinishedChooseBackgroundEvent(bitmap);
            }
        }

        
    }
}